package com.asm.controller.api;

import com.asm.dto.PageResult;
import com.asm.dto.Result;
import com.asm.service.CommunityService;
import com.asm.vo.sys.CommunityVo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;
import java.util.Map;

/**
 * @author morningbear
 */
@RestController
public class CommunityController {
    @Autowired
    CommunityService communityService;

    /**
     * 查询所有小区
     * @param pageNumber 页数
     * @param pageSize   每页大小
     * @return ResponseEntity<PageResult<CommunityVo>>
     */
    @GetMapping("/communities")
    public ResponseEntity<PageResult<CommunityVo>> getAllCommunities(@RequestParam(value = "pageNumber", defaultValue = "0") Integer pageNumber, @RequestParam(value = "pageSize", defaultValue = "15") Integer pageSize){
        return new ResponseEntity(new Result().ok("success", communityService.findAllCommunities(pageSize, pageNumber)), HttpStatus.OK);
    }

    /**
     * 根据小区类型查找小区
     * @param pageNumber 页数
     * @param pageSize   每页大小
     * @param type       小区类型
     * @return ResponseEntity<PageResult<CommunityVo>>
     */
    @GetMapping("/communities/type/{type}")
    public ResponseEntity<PageResult<CommunityVo>> getCommunityByType(@RequestParam(value = "pageNumber", defaultValue = "0") Integer pageNumber, @RequestParam(value = "pageSize", defaultValue = "15") Integer pageSize, @PathVariable String type){
        return new ResponseEntity(new Result().ok("success", communityService.findByCommunityType(type, pageSize, pageNumber)), HttpStatus.OK);
    }

    /**
     * 根据小区名字查找小区
     * @param name 小区名字
     * @return ResponseEntity<PageResult<CommunityVo>>
     */
    @GetMapping("/communities/name/{name}")
    public ResponseEntity<PageResult<CommunityVo>> getCommunityByName(@RequestParam(value = "pageNumber", defaultValue = "0") Integer pageNumber, @RequestParam(value = "pageSize", defaultValue = "15") Integer pageSize, @PathVariable String name){
        return new ResponseEntity(new Result().ok("success", communityService.findByCommunityName(name, pageSize, pageNumber)), HttpStatus.OK);
    }

    /**
     * 根据小区地址查找小区
     * @param address 小区地址
     * @return
     */
//    @GetMapping("/communities/address/{address}")
//    public List<Map<String, Object>> getCommunityByAddress(@PathVariable Point address){
//        return communityService.findByCommunityAddress(address);
//    }

    /**
     * 根据镇/区编号查找小区
     * @param districtId 编号
     * @return
     */
    @GetMapping("/communities/districtId/{districtId}")
    public ResponseEntity<PageResult<CommunityVo>> getCommunityByDistrictId(@RequestParam(value = "pageNumber", defaultValue = "0") Integer pageNumber, @RequestParam(value = "pageSize", defaultValue = "15") Integer pageSize, @PathVariable long districtId){
        return new ResponseEntity(new Result().ok("success", communityService.findByCommunityDistrictId(districtId, pageNumber, pageSize)), HttpStatus.OK);

    }

    /**
     * 根据商圈编号查找小区
     * @param tradeAreaId 编号
     * @return
     */
    @GetMapping("/communities/tradeAreaId/{tradeAreaId}")
    public  ResponseEntity<PageResult<CommunityVo>> getCommunityByTradeAreaId(@RequestParam(value = "pageNumber", defaultValue = "0") Integer pageNumber, @RequestParam(value = "pageSize", defaultValue = "15") Integer pageSize, @PathVariable long tradeAreaId){
        return new ResponseEntity(new Result().ok("success",  communityService.findByCommunityTradeAreaId(tradeAreaId, pageNumber, pageSize)), HttpStatus.OK);
    }

    /**
     * 根据编号查询某一个小区
     * @param id 小区编号
     * @return
     */
    @GetMapping("/community/{id}")
    public Map<String, Object> getCommunity(@PathVariable("id") Long id){
        return communityService.getCommunity(id);
    }

    /**
     * 小区添加
     * @param communityVo 小区
     * @return HttpStatus
     */
    @PostMapping("/community")
    public ResponseEntity<Result> addCommunity(@RequestBody CommunityVo communityVo){
        communityService.addCommunity(communityVo);
        return new ResponseEntity(new Result().ok("add success"), HttpStatus.OK);
    }

    /**
     * 小区删除
     * @param id 小区ID
     * @return HttpStatus
     */
    @DeleteMapping("/community/{id}")
    public ResponseEntity<Result> deleteCommunity(@PathVariable("id") Long id){
        communityService.deleteCommunity(id);
        return new ResponseEntity(new Result().ok("delete success"), HttpStatus.OK);
    }

    /**
     * 小区修改
     * @param communityVo 小区
     * @return HttpStatus
     */
    @PutMapping("/community")
    public ResponseEntity<Result> updateCommunity(@RequestBody CommunityVo communityVo){
        communityService.updateCommunity(communityVo);
        return new ResponseEntity(new Result().ok("register success"), HttpStatus.OK);
    }
}
